package vn.smartoffice.promail.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang.StringUtils;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zkplus.spring.DelegatingVariableResolver;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import vn.smartoffice.auth.UserService;
import vn.smartoffice.promail.dto.MailContactGroupNode;
import vn.smartoffice.promail.entity.MailContact;
import vn.smartoffice.promail.services.MailContactService;

@VariableResolver(DelegatingVariableResolver.class)
public class MailContactCtr {

	@WireVariable private UserService userService;
	@WireVariable private MailContactService mailContactService;

	@Getter @Setter private String keyword;
	@Getter @Setter private ListModelList<MailContact> contactListModel;
	@Getter @Setter private MailContact selectedContact;
	private long groupId;

	@GlobalCommand
	@NotifyChange("contactListModel")
	public void showMailContactList(@BindingParam("base") MailContactGroupNode base) {
		this.groupId = base.getId();
		List<MailContact> listContacts = mailContactService.findByUserGroupId(userService.getCurrentUserId(),
				base.getId());
		contactListModel = new ListModelList<MailContact>(listContacts);
	}

	@Command("search")
	@NotifyChange({ "contactListModel", "selectedContact" })
	public void search() {
		if (keyword != null) {
			List<MailContact> listContacts = mailContactService.findByKeyword(StringUtils.trimToEmpty(keyword));
			contactListModel = new ListModelList<MailContact>(listContacts);
		}
	}

	@Command("edit")
	public void edit() {
		Map<String, Object> dataArgs = new HashMap<>();
		dataArgs.put("maiContact", selectedContact);
		Window window = (Window) Executions.createComponents("dialog/contact.zul", null, dataArgs);
		window.doModal();
	}

	@Command("delete")
	public void delete() {
		Messagebox.show(Labels.getLabel("delete.messages"), Labels.getLabel("delete.title"), Messagebox.OK
				| Messagebox.CANCEL, Messagebox.QUESTION, new EventListener<Event>() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getName().equals("onOK")) {
					contactListModel.remove(selectedContact);
					mailContactService.deleteById(selectedContact.getId());
					selectedContact = null;
					BindUtils.postNotifyChange(null, null, MailContactCtr.this, "selectedContact");
					BindUtils.postNotifyChange(null, null, MailContactCtr.this, "contactListModel");
				}
			}
		});
	}

	@GlobalCommand
	@NotifyChange({ "contactListModel", "selectedContact" })
	public void refreshMailContact(@BindingParam("contactId") MailContact mailContact) {
		List<MailContact> listContacts = mailContactService.findByUserGroupId(userService.getCurrentUserId(), groupId);
		contactListModel = new ListModelList<MailContact>(listContacts);
		if (mailContact != null) {
			selectedContact = mailContact;
		} else if (contactListModel.size() > 0) {
			selectedContact = contactListModel.get(0);
		}
	}

}
